home *** CD-ROM | disk | FTP | other *** search
/ MacWorld 2003 August / MW 8 2003 CD1.iso / Inside Macworld / Product News / gimp-1.2.4.sit / gimp-1.2.4 / plug-ins / perl / examples / dust < prev    next >
Encoding:
Text File  |  2000-08-24  |  3.4 KB  |  139 lines

  1. #!/usr/app/bin/perl
  2.  
  3. eval 'exec /usr/app/bin/perl  -S $0 ${1+"$@"}'
  4.     if 0; # not running under some shell
  5.  
  6. use Gimp;
  7. use Gimp::Fu;
  8. use Gimp::Util;
  9.  
  10. # this is silly, sorry that I am too dumb to come up with niftier functions :(
  11. # btw, this should generate something between 1/f and 1/f≤ noise.
  12. {
  13.    my @weight = (1/8, 1/4, 1/2, 1);
  14.    my @dice;
  15.    my $total = 0;
  16.    my $tweight = 0;
  17.    my $n = 0;
  18.    my $seed;
  19.  
  20.    sub gen_rand() {
  21.       my ($prevrand, $newrand, $k);
  22.  
  23.       $n++;
  24.       for (0..$#weight) {
  25.          if ($n & (1<<$_)) {
  26.             $prevrand = $dice[$_];
  27.             $newrand = rand() * $weight[$_];
  28.             $dice[$_] = $newrand;
  29.             $total += $newrand - $prevrand;
  30.          }
  31.       }
  32.  
  33.       $total / $tweight;
  34.    }
  35.  
  36.    sub set_seed($) {
  37.       if ($_[0]) {
  38.          srand $_[0];
  39.       } else {
  40.          srand;
  41.       }
  42.       $total = 0;
  43.       $tweight = 0;
  44.       for (0..$#weight) {
  45.          $dice[$_] = rand()*$weight[$_];
  46.          $total += $dice[$_];
  47.          $tweight += $weight[$_];
  48.       }
  49.    }
  50. }
  51.  
  52. # the next line  just shows a graph of the "random" numbers.
  53. #set_seed 0; use PDL; use PDL::Graphics::PGPLOT; line(pdl(float,map gen_rand, 1..500));
  54.  
  55. register "dust",
  56.          "",
  57.          "",
  58.          "Marc Lehmann",
  59.          "Marc Lehmann <pcg\@goof.com",
  60.          "0.1",
  61.          N_"<Image>/Filters/Render/Add Dust...",
  62.          "*",
  63.          # Eingabeparameter
  64.          # Typ        Name        Beschreibung        Wert
  65.          [
  66.           [PF_FLOAT,    'density',    'dust density in dust/pixel',     0.0001],
  67.           [PF_INT32,    'seed',        'the random seed (0 == unspecified)', 0],
  68.           [PF_SPINNER,    'length',    'the average dust corn length', 50, [1,300]],
  69.          ],
  70.          sub {                    # Perl-Code
  71.    # Die Parameter werden ganz "normal" ¸bergeben:
  72.    my ($image, $layer, $density, $seed, $len) = @_;
  73.  
  74.    $len *= 0.75;
  75.  
  76.    set_seed $seed;
  77.  
  78.    my ($w, $h) = ($image->width, $image->height);
  79.  
  80.    $image->undo_push_group_start;
  81.  
  82.    my $state = Gimp::Util::get_state();
  83.    Palette->set_foreground("white");
  84.    Brushes->set_brush("Circle (01)");
  85.    Brushes->set_opacity(50);
  86.    Brushes->set_spacing(100);
  87.    Brushes->set_paint_mode(NORMAL_MODE);
  88.  
  89.    if (1) {
  90.       $layer = $image->add_new_layer (0, TRANS_IMAGE_FILL, 1);
  91.       $layer->set_mode(DIFFERENCE_MODE);
  92.    }
  93.    
  94.    for (1..($w*$h*$density)) {
  95.       my ($x, $y) = (rand $w, rand $h);
  96.       my $l = int($len + rand $len);
  97.       my @c;
  98.       my $b = 0;
  99.       for (1..$l) {
  100.          push @c, $b += 5*(gen_rand-0.5);
  101.          push @c, $b += 5*(gen_rand-0.5);
  102.       }
  103.       
  104.       $layer->paintbrush_default([map { $x+$c[$_], $y+$c[$_+$l] } 0..$l-1]);
  105.    }
  106.  
  107.    Gimp::Util::set_state($state);
  108.  
  109.    $image->undo_push_group_end;
  110.  
  111.    ();
  112. };
  113.  
  114. #register "gen_rand_1f",
  115. #         "generate 1/f noise",
  116. #         "Generate approximate 1/f (white) noise in the range [0,1[",
  117. #         "Marc Lehmann",
  118. #         "Marc Lehmann <pcg\@goof.com",
  119. #         "0.1",
  120. #         "<None>",
  121. #         undef,
  122. #         # Eingabeparameter
  123. #         # Typ        Name        Beschreibung        Wert
  124. #         [
  125. #          [PF_FLOAT,    'count',    'the number of values',     1],
  126. #          [PF_INT32,    'seed',        'the random seed (0 == unspecified)', 0],
  127. #         ],
  128. #         [
  129. #          [&Gimp::PDB_FLOATARRAY,'noise','the requested number of 1/f noise values'],
  130. #         ],
  131. #         sub {
  132. #            my ($count, $seed) = @_;
  133. #            set_seed $seed;
  134. #            [map gen_rand_1f, 1..$count];
  135. #         };
  136.  
  137. exit main;
  138.  
  139.